local shicai = fk.CreateSkill {

  name = "joy__shicai",

  tags = {  },

}



shicai:addEffect(fk.CardUseFinished, {
  name = "joy__shicai",
  events = {fk.CardUseFinished, fk.TargetConfirmed},
  anim_type = "drawCard",
  can_trigger = function(self, event, target, player, data)
    if
      not (
        target == player and
        player:hasSkill(shicai.name) and
        (data.extra_data or {}).firstCardTypeUsed and
        player.room:getCardArea(data.card) == Card.Processing
      )
    then
      return false
    end
    if event == fk.CardUseFinished then
      return data.card:isCommonTrick() or data.card.type == Card.TypeBasic
    else
      return data.from == player.id and data.card.type == Card.TypeEquip
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local toPut = room:getSubcardsByRule(data.card, { Card.Processing })
    if #toPut > 1 then
      toPut = room:askForGuanxing(player, toPut, { #toPut, #toPut }, { 0, 0 }, "joy__ShiCaiPut", true).top
    end
    room:moveCardTo(table.reverse(toPut), Card.DrawPile, nil, fk.ReasonPut, shicai.name, nil, true)
    player:drawCards(1, shicai.name)
  end,
})
shicai:addEffect(fk.TargetConfirmed, {
  name = "joy__shicai",
  events = {fk.CardUseFinished, fk.TargetConfirmed},
  anim_type = "drawCard",
  can_trigger = function(self, event, target, player, data)
    if
      not (
        target == player and
        player:hasSkill(shicai.name) and
        (data.extra_data or {}).firstCardTypeUsed and
        player.room:getCardArea(data.card) == Card.Processing
      )
    then
      return false
    end
    if event == fk.CardUseFinished then
      return data.card:isCommonTrick() or data.card.type == Card.TypeBasic
    else
      return data.from == player.id and data.card.type == Card.TypeEquip
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local toPut = room:getSubcardsByRule(data.card, { Card.Processing })
    if #toPut > 1 then
      toPut = room:askForGuanxing(player, toPut, { #toPut, #toPut }, { 0, 0 }, "joy__ShiCaiPut", true).top
    end
    room:moveCardTo(table.reverse(toPut), Card.DrawPile, nil, fk.ReasonPut, shicai.name, nil, true)
    player:drawCards(1, shicai.name)
  end,
})
shicai:addEffect(fk.EventPhaseStart, {
  refresh_events = {fk.EventPhaseStart, fk.AfterCardUseDeclared},
  can_refresh = function(self, event, target, player, data)
    if target ~= player then
      return false
    end
    if event == fk.EventPhaseStart then
      return
        player.phase == Player.NotActive and
        table.find(player.room.alive_players, function(p)
          return type(p:getMark("@joy__shicai")) == "table"
        end)
    else
      return
        player:hasSkill(shicai.name, true) and
        (type(player:getMark("@joy__shicai")) ~= "table" or
        not table.contains(player:getMark("@joy__shicai"), data.card:getTypeString() .. "_char"))
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      for _, p in ipairs(room.alive_players) do
        if type(p:getMark("@joy__shicai")) == "table" then
          room:setPlayerMark(p, "@joy__shicai", 0)
        end
      end
    else
      local typesRecorded = type(player:getMark("@joy__shicai")) == "table" and player:getMark("@joy__shicai") or {}
      table.insert(typesRecorded, data.card:getTypeString() .. "_char")
      room:setPlayerMark(player, "@joy__shicai", typesRecorded)
      data.extra_data = data.extra_data or {}
      data.extra_data.firstCardTypeUsed = true
    end
  end,
})
shicai:addEffect(fk.AfterCardUseDeclared, {
  refresh_events = {fk.EventPhaseStart, fk.AfterCardUseDeclared},
  can_refresh = function(self, event, target, player, data)
    if target ~= player then
      return false
    end
    if event == fk.EventPhaseStart then
      return
        player.phase == Player.NotActive and
        table.find(player.room.alive_players, function(p)
          return type(p:getMark("@joy__shicai")) == "table"
        end)
    else
      return
        player:hasSkill(shicai.name, true) and
        (type(player:getMark("@joy__shicai")) ~= "table" or
        not table.contains(player:getMark("@joy__shicai"), data.card:getTypeString() .. "_char"))
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      for _, p in ipairs(room.alive_players) do
        if type(p:getMark("@joy__shicai")) == "table" then
          room:setPlayerMark(p, "@joy__shicai", 0)
        end
      end
    else
      local typesRecorded = type(player:getMark("@joy__shicai")) == "table" and player:getMark("@joy__shicai") or {}
      table.insert(typesRecorded, data.card:getTypeString() .. "_char")
      room:setPlayerMark(player, "@joy__shicai", typesRecorded)
      data.extra_data = data.extra_data or {}
      data.extra_data.firstCardTypeUsed = true
    end
  end,
})

return shicai